#tag Module
Protected Module AIRunner
	#tag Method, Flags = &h21
		Private Function RandomVsDecission() As game.ConcreteGame
		  dim pd as PlayerData
		  dim theGame as game.ConcreteGame
		  dim r as integer = 0
		  dim d as integer = 0
		  
		  theGame = game.ConcreteGame.GetNewGameInstance
		  
		  
		  pd.name = "Random"
		  pd.type = 2
		  pd.aiType = 1
		  
		  theGame.AddPlayer pd
		  
		  pd.name = "Decission"
		  pd.type = 2
		  pd.aiType = 2
		  
		  theGame.AddPlayer pd
		  
		  theGame.Go
		  
		  d = theGame.GetPlayerByName("Decission").GetScore
		  r = theGame.GetPlayerByName("Random").GetScore
		  
		  if d > app.highestD then
		    app.highestD = d
		  end if
		  if r > app.highestR then
		    app.highestR = r
		  end if
		  
		  if d > r then
		    app.decissionAI = app.decissionAI + 1
		  else
		    if d < r then
		      app.randomAI = app.randomAI + 1
		    else
		      app.tie = app.tie + 1
		    end if
		  end if
		  
		  'unitTests.TestPassed app.Dbg
		  
		  theGame.WriteLog(app.Dbg)
		  
		  app.Dbg = ""
		  
		  
		  
		  
		  return theGame
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Run()
		  dim runs as integer
		  dim type as string
		  dim gf as GameFacade
		  dim game as game.ConcreteGame
		  dim allGames() as game.ConcreteGame
		  dim i as integer
		  
		  gf = app.GetGameFacade
		  
		  gf.IsViewable = false
		  gf.Reset
		  
		  winAiRun.lbxGames.DeleteAllRows
		  
		  runs = winAIRun.runs
		  type = winAIRun.type
		  
		  if type = "" then
		    dim m as new MessageDialog
		    
		    m.Title = "No AIRunner active!"
		    m.Message = "There is no active AI-Runner."
		    m.Explanation = "Please choose an AI-Runner from the AI-menu."
		    m.ActionButton.Caption = "Ok"
		    
		    call m.ShowModalWithin(winAIRun)
		  else
		    
		    app.decissionAI = 0
		    app.randomAI = 0
		    app.tie = 0
		    app.highestR = 0
		    app.highestD = 0
		    
		    winAIRun.prgAI.Value = 0
		    winAiRun.prgAi.Maximum = runs
		    winAIRun.edtMaxAI1.text = ""
		    winAIRun.edtMaxAI2.text = ""
		    
		    for i = 1 to runs
		      select case type
		      case "RvD"
		        game = RandomVsDecission
		      else
		        //raise error
		        //todo
		      end select
		      game.AddGameNo i
		      winAIRun.prgAI.Value = i
		      winAIRun.prgAI.Refresh
		      gf.AddAiGame game
		      gf.SetAIGame game
		      winAIRun.lbxGames.AddRow("Game number " + str(i) + "(" +  game.GetPlayerById(0).GetName + " " + str(game.GetPlayerById(0).GetScore) + " / "  _
		      + game.GetPlayerById(1).GetName + " " + str(game.GetPlayerById(1).GetScore)) + ")"
		    next
		    
		    gf.IsViewable = true
		    
		    winAIRun.txtAI1Name.Text = game.GetPlayerById(0).GetName
		    winAIRun.txtAI2Name.Text = game.GetPlayerById(1).GetName
		    
		    winAIRun.edtScoreAI1.Text = str(game.GetPlayerById(0).GetScore)
		    winAIRun.edtScoreAI2.Text = str(game.GetPlayerById(1).GetScore)
		    
		    winAiRun.prgAI.Value = runs
		    gf.ShowAIGame
		    
		    winAIRun.edtMaxAI1.Text = str(app.highestR)
		    winAIRun.edtMaxAI2.Text = str(app.highestD)
		    
		    dim m as new MessageDialog
		    
		    m.Title = "Total Wins after " + str(runs) + " games"
		    m.Message = "AI 1: " + str(app.randomAI) + EndOfLine + "AI 2: " + str(app.decissionAI) + EndOfLine + "Ties: " + str(app.tie)
		    m.Explanation = "Highest Score AI 1: " + str(app.highestR) + EndOfLine + "Highest Score AI 2: " + str(app.highestD)
		    m.ActionButton.Caption = "Ok"
		    call m.ShowModalWithin (winAIRun)
		    
		  end if
		  
		End Sub
	#tag EndMethod


	#tag ViewBehavior
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			InitialValue="-2147483648"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
	#tag EndViewBehavior
End Module
#tag EndModule
